package com.djhu.freepartner.web.control;

import com.baomidou.mybatisplus.plugins.Page;
import com.djhu.freepartner.bean.PartnerPost;
import com.djhu.freepartner.bean.PartnerPostType;
import com.djhu.freepartner.bean.PartnerUser;
import com.djhu.freepartner.core.RestRsp;
import com.djhu.freepartner.core.Status;
import com.djhu.freepartner.core.exception.ServiceException;
import com.djhu.freepartner.service.IPartnerPostService;
import com.djhu.freepartner.service.IPartnerUserService;
import com.djhu.freepartner.utils.DateUtils;
import com.djhu.freepartner.utils.GeneralUtils;
import com.djhu.freepartner.web.control.model.PostSearchModel;
import com.djhu.freepartner.web.control.model.ReadPostRequest;
import com.djhu.freepartner.web.service.PostService;
import com.google.gson.Gson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/post/")
@Api("找个陪玩帖子api")
@Slf4j
public class PartnerPostControl {
    @Autowired
    private IPartnerPostService partnerPostService;

    @Autowired
    private IPartnerUserService partnerUserService;

    @Autowired
    private PostService postService;


    @Autowired
    private Gson gson;

    @ApiOperation(value = "用户发布 陪玩", notes = "用户发布 陪玩接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "partnerPost", value = "陪玩帖子内容", required = true, dataType = "PartnerPost")
    })
    @PostMapping(value = "/publishfindpartnerPostfree")
    public RestRsp publishfindpartnerPostfree(@RequestBody PartnerPost partnerPost) throws ServiceException {
        partnerPost.setPostId(GeneralUtils.getUUID());
        partnerPost.setPostType(PartnerPostType.FINDPARTNER.getType());
        partnerPost.setBrowse(0);
        partnerPost.setCreateDate(DateUtils.now());
        partnerPost.setModifyDate(DateUtils.now());
        partnerPost.setTop(0);
        partnerPost.setEssence(0);
        String userid = partnerPost.getCreateUserId();
        PartnerUser partnerUser = partnerUserService.selectById(userid);
        if (partnerUser == null){
            throw new ServiceException(Status.BAD_PARAM);
        }
        partnerPost.setCreateUserusername(partnerUser.getUsername());

        RestRsp restRsp = postService.pubishPost(partnerPost,false);
        return restRsp;
    }

    @ApiOperation(value = "用户发布 出租自己", notes = "用户发布 陪玩接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "partnerPost", value = "陪玩帖子内容", required = true, dataType = "PartnerPost")
    })
    @PostMapping(value = "/publishlendselfPostfree")
    public RestRsp publishlendselfPostfree(@RequestBody PartnerPost partnerPost) throws ServiceException {
        partnerPost.setPostId(GeneralUtils.getUUID());
        partnerPost.setPostType(PartnerPostType.LENDSELF.getType());
        partnerPost.setBrowse(0);
        partnerPost.setCreateDate(DateUtils.now());
        partnerPost.setModifyDate(DateUtils.now());
        partnerPost.setTop(0);
        partnerPost.setEssence(0);
        String userid = partnerPost.getCreateUserId();
        PartnerUser partnerUser = partnerUserService.selectById(userid);
        if (partnerUser == null){
            throw new ServiceException(Status.BAD_PARAM);
        }
        partnerPost.setCreateUserusername(partnerUser.getUsername());
        boolean insert = partnerPostService.insert(partnerPost);
        if (insert){
            RestRsp restRsp = new RestRsp();
            restRsp.setCode(Status.SUCCESS.getCode());
            restRsp.setMsg("发布成功");
            return restRsp;
        }else {
            log.info("发布帖子失败");
            throw new ServiceException(Status.SERVER_ERROR);
        }
    }


    @ApiOperation(value = "用户读取帖子", notes = "用户读取帖子")
    @PostMapping(value = "/readpost")
    public RestRsp readpost(@RequestBody ReadPostRequest readPostRequest) throws ServiceException {
        log.info("readpost post request is {}",readPostRequest);
        PartnerPost partnerPost = postService.readPost(readPostRequest);
        RestRsp restRsp = new RestRsp(Status.SUCCESS);
        if (partnerPost != null){
            restRsp.setData(partnerPost);
            return restRsp;
        }else {
            restRsp = new RestRsp(Status.NOT_FOUND);
            return restRsp;
        }
    }


    @ApiOperation(value = "用户发布帖子 之前检查", notes = "用户发布帖子 之前检查")
    @PostMapping("/readPostCheck")
    public RestRsp readPostCheck(@RequestBody PostSearchModel postSearchModel) throws ServiceException{
        String userid = postSearchModel.getUserid();
        String postid = postSearchModel.getPostid();
        log.info("publishPostCheck receive userid is {},postid is {}",userid,postid);
        if (StringUtils.isAnyEmpty(userid) ){
            throw new ServiceException(Status.SERVER_ERROR);
        }
        return postService.readpostCheck(userid,postid);
    }


    @ApiOperation(value = "用户发布帖子 之前检查", notes = "用户发布帖子 之前检查")
    @PostMapping("/publishpostcheck")
    public RestRsp publishPostCheck(@RequestBody PostSearchModel postSearchModel) throws ServiceException{
        String userid = postSearchModel.getUserid();
        log.info("publishPostCheck receive userid is {}",userid);
        if (StringUtils.isEmpty(userid)){
            throw new ServiceException(Status.SERVER_ERROR);
        }
        log.info("publishPostCheck check ,userid is {}",userid);
        return postService.publishpostCheck(userid);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiOperation(value="用户删帖子", notes="用户删帖子")
    public RestRsp deletePost(@RequestBody PostSearchModel postSearchModel) {
        log.info("deletePost receive request is {}",gson.toJson(postSearchModel));
        boolean success = postService.deletePost(postSearchModel);
        if (success){
            return new RestRsp(Status.SUCCESS);
        }else {
            return new RestRsp(Status.SERVER_ERROR);
        }
    }

    @ApiOperation(value="列出用户的帖子", notes="列出用户的帖子")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public RestRsp list(@RequestBody PostSearchModel postSearchModel) {
        List<PartnerPost> postList  =  postService.list(postSearchModel.getUserid());
        RestRsp restRsp = new RestRsp(Status.SUCCESS);
        restRsp.setData(postList);
        return restRsp;
    }


    @RequestMapping(value = "/search", method = RequestMethod.POST)
    @ApiOperation(value="玩伴查询界面", notes="玩伴查询界面")
    public RestRsp searchPost(@RequestBody PostSearchModel postSearchModel) {
        log.info("searchpost receive request is {}",gson.toJson(postSearchModel));
        Page<PartnerPost> partnerPostPage = postService.searchPost(postSearchModel);
        RestRsp restRsp = new RestRsp();
        restRsp.setData(partnerPostPage);
        return restRsp;
    }


//    @RequestMapping(value = "/postInfo", method = RequestMethod.POST)
//    @ApiOperation(value="查看玩伴帖子详细内容", notes="查看玩伴帖子详细内容")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "postId", value = "帖子的ID", required = true, dataType = "String")
//    })
//    public RestRsp searchPostInfo(String postId) {
//        PartnerPost partnerPost = partnerPostService.selectById(postId);
//        RestRsp restRsp = new RestRsp();
//        restRsp.setCode(Status.SUCCESS.getCode());
//        restRsp.setData(partnerPost);
//        return restRsp;
//    }
}
